package com.tibco.gwt.core.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.dom.client.LIElement;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.OListElement;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.dom.client.UListElement;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.impl.DOMImpl;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.tibco.gwt.core.client.ui.data.FilmStripData;

public class FilmIcon extends Composite {

	private static FilmIconUiBinder uiBinder = GWT
			.create(FilmIconUiBinder.class);
	@UiField
	HTMLPanel panel;
	@UiField 
	UListElement liContainer;
	private List list;

	interface FilmIconUiBinder extends UiBinder<Widget, FilmIcon> {
	}

	public FilmIcon() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public FilmIcon(List<FilmStripData> list) {
		this.list = list;
		initWidget(uiBinder.createAndBindUi(this));
	}

	public @UiHandler("panel")
	void onPanelAttachOrDetach(AttachEvent event) {
		// String innerHTMLstr = "";
		for (int i = 0; i < list.size(); i++) {
			FilmStripData fsd = new FilmStripData();
			fsd = (FilmStripData) list.get(i);
			LIElement li = Document.get().createLIElement();
			AnchorElement a = Document.get().createAnchorElement();
			a.setAttribute("href", "#");
			li.appendChild(a);
			ImageElement image = Document.get().createImageElement();
			image.setSrc(fsd.getImgUrl());
		    ImgEventListener listener = new ImgEventListener("click");
			Event.setEventListener(image, listener);
			image.dispatchEvent(Document.get().createClickEvent(0, 0, 0, 0, 0, false,
			       false, false, false));
			// image.setSrc(fsd.getImgUrl());
			// SimplePanel panel = new SimplePanel();
/*			Image img = new Image(fsd.getImgUrl());
			img.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					Window.alert("Hello, again");
				}
			});
			*/
			// SimplePanel panel = new SimplePanel();
			 //panel.add(image);
			SpanElement span = Document.get().createSpanElement();
			span.setInnerText(fsd.getName());
			a.appendChild(image);
			a.appendChild(span);
			// li.appendChild(panel.getElement());
			liContainer.appendChild(li);
			// innerHTMLstr+="<li><a href=\"#\"><img src=\""+fsd.getImgUrl()+"\"/><span>"+fsd.getName()+"</span></a></li>";
		}
		
		/*
		 * liContainer.setInnerHTML(innerHTMLstr); NodeList list=
		 * liContainer.getElementsByTagName("img"); for(int
		 * i=0;i<list.getLength();i++){
		 * 
		 * //ImageElement.as(Element.as(list.getItem(i))).dispatchEvent() }
		 */
	}
	  /**
	   * An event listener that asserts that events are received properly for the
	   * img element.
	   */
	  private static class ImgEventListener implements EventListener {
	    private boolean imgReceived;
	    private final String eventType;

	    public ImgEventListener(String eventType) {
	      this.eventType = eventType;
	    }

	    public void onBrowserEvent(Event event) {
	    	Window.alert("success! hooooooooooo");
	      if (event.getType().equals(eventType)) {
	    	  Window.alert("success! hooooooooooo");
	      }
	    }
	  }

}
